﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using XpressNews.SvcRef.CateMgmt;
using XpressNews.SvcRef.NewsMgmt;

namespace XpressNews
{
    public partial class Index : System.Web.UI.Page
    {
        private ServiceCategoryManagementClient cCat = null;
        private ServiceNewsManagementClient cNews = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadCat();
            }
        }

        public void rptCategory_OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                String catId = ((Category)e.Item.DataItem).catId;

                Repeater rpt = ((Repeater)e.Item.FindControl("rptNews"));
                rpt.DataSource = cNews.RetrieveNewsByCategoryID(catId);
                rpt.DataBind();
            }
        }

        private void loadCat()
        {
            cCat = new ServiceCategoryManagementClient("catBasicHttpBinding");
            var lstCategory = cCat.searchAllCategories();
            cCat.Close();

            if (lstCategory != null)
            {
                var lst = lstCategory.Where(i => String.IsNullOrEmpty(i.catParentId)).ToList<Category>();

                rptCategory.DataSource = lst;
                rptCategory.DataBind();
            }
        }
    }
}